/*!
\copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.\n
            All Rights Reserved.\n
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file       
\brief      The sending of events from the Connection Manager using the Event Bus
*/

#include "pairing_event_send.h"

#include "ble_link_channel_event_spec.h"
#include "conn_acl_status_channel_event_spec.h"
#include "connection_manager.h"
#include "event_bus_publish.h"

#include <logging.h>

static void pairing_EventBusSendAclStatus(conn_acl_status_event_t event, void * data, uint16 data_size)
{
    EventBus_Publish(EVENT_BUS_CHANNEL_CONN_ACL_STATUS, event, data, data_size);
}

static void pairing_EventBusSendBleLink(ble_link_event_t event, void * data, uint16 data_size)
{
    EventBus_Publish(EVENT_BUS_CHANNEL_BLE_LINK, event, data, data_size);
}

static void Pairing_EventSendSimplePairingComplete(const tp_bdaddr * tpaddr, hci_status status)
{
    CONN_ACL_SIMPLE_PAIRING_COMPLETE_T event = {0};
    if (tpaddr)
    {
        event.tpaddr = *tpaddr;
    }
    else
    {
        BdaddrTpSetEmpty(&event.tpaddr);
    }
    event.status = status;
    
    pairing_EventBusSendAclStatus(CONN_ACL_SIMPLE_PAIRING_COMPLETE, &event, sizeof(CONN_ACL_SIMPLE_PAIRING_COMPLETE_T));
}

void Pairing_EventSendSimplePairingCompleteWithBdaddr(const bdaddr * addr, hci_status status)
{
    tp_bdaddr tpaddr;
    
    if (addr)
    {
        BdaddrTpFromBredrBdaddr(&tpaddr, addr);        
        Pairing_EventSendSimplePairingComplete(&tpaddr, status);
    }
    else
    {
        Pairing_EventSendSimplePairingComplete(NULL, status);
    }
}

void Pairing_EventSendSimplePairingCompleteWithTpaddr(const tp_bdaddr * tpaddr, hci_status status)
{
    Pairing_EventSendSimplePairingComplete(tpaddr, status);
}

void Pairing_EventSendBleEncryptionChanged(const tp_bdaddr * tpaddr)
{
    ble_link_event_encryption_changed_t event = {0};
    
    event.taddr = tpaddr->taddr;
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    
    pairing_EventBusSendBleLink(ble_link_event_encryption_changed, &event, sizeof(ble_link_event_encryption_changed_t));
}

void Pairing_EventSendBleBond(const tp_bdaddr * tpaddr)
{
    ble_link_event_bond_t event = {0};
    
    event.taddr = tpaddr->taddr;
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    
    pairing_EventBusSendBleLink(ble_link_event_bond, &event, sizeof(ble_link_event_bond_t));
}
